const DB = require('../db/index');
const enumData = require('../enumeration/index');
const exception = require('../exception');
const fw = require('../core/framework');

module.exports = {
    create(data, successFn, failFn) {
        try {
            DB.AlarmEvent.create(data).then((res) => {
                successFn({code: 0,data:res})
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    update(data, successFn, failFn) {
        try {
            DB.AlarmEvent.update(data).then(() => {
                successFn({code: 0})
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    search(data, successFn, failFn) {
        try {
            DB.AlarmEvent.searchAll(data).then((res) => {
                successFn({
                    code: 0, data: res
                });
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    untreatedSearch(data, successFn, failFn) {
        try {
            data.handle_type = 2;
            DB.AlarmEvent.untreatedSearch(data).then((res) => {
                successFn({
                    code: 0, data: (res||[]).length
                });
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    getLastEvent(data,successFn,failFn){
        try {
            DB.AlarmEvent.getLastEvent({device_id: data.device_id}).then((dbResData) => {
                let dbRes = fw.getDBObject(dbResData);
                if (fw.isExit(dbRes)) {
                    if (fw.isFn(successFn)) successFn(dbRes);
                } else {
                    if (fw.isFn(failFn)) failFn();
                }
            })
        } catch (e) {
            if (fw.isFn(failFn)) failFn(e);
        }
    }
};
